package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;


import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.CheckboxTypeMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.RadioTypeMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.*;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.CheckboxType;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.RadioType;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomOperatingUnitFormItemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作单元表单数据项
 * Created by yangcs on 2017/2/8.
 */
public class SomOperatingUnitFormItemServiceImpl implements SomOperatingUnitFormItemService {
    @Autowired
    private OperatingUnitStepMapper operatingUnitStepMapper;
    @Autowired
    private OperatingStepDataItemMapper operatingStepDataItemMapper;
    @Autowired
    private RadioTypeMapper radioTypeMapper;
    @Autowired
    private CheckboxTypeMapper checkboxTypeMapper;
    @Autowired
    private SomOperatingUnitFormMapper somOperatingUnitFormMapper;
    @Autowired
    private OperatingUnitFormItemMapper operatingUnitFormItemMapper;
    @Autowired
    private SomOperatingUnitFormFatherMapper somOperatingUnitFormFatherMapper;
    @Autowired
    private SomFileService fileService;

    /**
     * 根据查询条件查询步骤信息（含结果）
     *
     * @param operatingUnitStep 查询条件
     * @param cellResultId
     * @return
     */
    @Override
    public List<OperatingUnitStep> getStepByParams(OperatingUnitStep operatingUnitStep, String cellResultId) {
        // 获取操作步骤
        Map map = new HashMap<>();
        map.put("version", operatingUnitStep.getVersion());
        map.put("cellId", operatingUnitStep.getCellId());
        map.put("workContentId", operatingUnitStep.getWorkContentId());
        map.put("cellResultId", StringUtils.isBlank(cellResultId) ? "-1" : cellResultId);
        List<OperatingUnitStep> lous = operatingUnitStepMapper.selectById(map);
        List<OperatingUnitStep> newLous = new ArrayList<>();
        // 不为空时执行循环操作
        if (lous != null && lous.size() > 0) {
            // 循环操作步骤
            for (OperatingUnitStep ous : lous) {
                // 步骤图片
                ous.setFiles(fileService.findByBusinessId(ous.getStepId()));
                // 获取操作步骤数据项
                List<OperatingStepDataItem> losd = operatingStepDataItemMapper.selectByStepId(ous);
                // 不为空时执行循环操作
                if (losd != null && losd.size() > 0) {
                    // 循环操作步骤数据项看是什么类型
                    for (OperatingStepDataItem osdi : losd) {
                        // 如果是2是单选,查询复选表并赋值
                        if (osdi.getStepDataItemType() == 2) {
                            // 查询单选对象
                            List<RadioType> radioType = radioTypeMapper.selectByDataItemId(osdi.getDataItemId());
                            // 设置属性
                            osdi.setRadioTypeList(radioType);
                            // 如果是3是复选,查询复选表并赋值
                        } else if (osdi.getStepDataItemType() == 3) {
                            // 查询复选框对象
                            List<CheckboxType> checkboxTypes = checkboxTypeMapper.selectByDataItemId(osdi.getDataItemId());
                            // 设置属性
                            osdi.setCheckboxTypeList(checkboxTypes);
                        }
                    }
                    // 设置操作单元步骤数据录取项
                    ous.setOperatingStepDataItemList(losd);
                }
                newLous.add(ous);
            }
        }
        return newLous;
    }

    /**
     * 根据cellId查询操作单元表单（含结果）
     *
     * @param cellId
     * @param cellResultId
     * @return
     */
    @Override
    public OperatingUnitForm getUnitFormByCellId(String cellId, String cellResultId, BigDecimal version) {
        // 获取操作单元表单
        Map map = new HashMap<>();
        map.put("cellId", cellId);
        map.put("version", version);
        OperatingUnitForm operatingUnitForm = somOperatingUnitFormMapper.getUnitFormByCellId(map);
        if (operatingUnitForm != null && StringUtils.isNotBlank(operatingUnitForm.getCellFormId())) {
            // 获取操作单元表单表单项
            List<OperatingUnitFormFather> louff = somOperatingUnitFormFatherMapper.queryByCellFormId(operatingUnitForm.getCellFormId());
            List<OperatingUnitFormFather> newLouff = new ArrayList<>();
            // 操作单元表单数据项
            List<OperatingUnitFormItem> loufi;
            List<OperatingUnitFormItem> newLoufi;
            // 不为空时执行循环操作
            if (louff != null && louff.size() > 0) {
                for (OperatingUnitFormFather ouff : louff) {
                    // 获取操作单元表单数据项
                    loufi = queryByCellFormItemFatherId(ouff.getCellFormItemFatherId(), cellResultId);
                    newLoufi = new ArrayList<>();
                    // 不为空时执行循环操作
                    if (loufi != null && loufi.size() > 0) {
                        for (OperatingUnitFormItem oufi : loufi) {
                            // 如果是2是单选,查询复选表并赋值
                            if (oufi.getCellFormItemType() == 2) {
                                // 查询单选对象
                                List<RadioType> radioType = radioTypeMapper.selectByDataItemId(oufi.getCellFormItemId());
                                // 设置属性
                                oufi.setRadioTypeList(radioType);
                                // 如果是3是复选,查询复选表并赋值
                            } else if (oufi.getCellFormItemType() == 3) {
                                // 查询复选框对象
                                List<CheckboxType> checkboxTypes = checkboxTypeMapper.selectByCellFormItemId(oufi.getCellFormItemId());
                                // 设置属性
                                oufi.setCheckboxTypeList(checkboxTypes);
                            }
                            newLoufi.add(oufi);
                        }
                    }
                    ouff.setLoufi(newLoufi);
                    newLouff.add(ouff);
                }
            }
            // 操作单元表单表单项赋值
            operatingUnitForm.setLouff(newLouff);
        }
        return operatingUnitForm;
    }

    /**
     * 查询操作单元表单数据项
     *
     * @param cellFormItemFatherId 表单项ID
     * @param cellResultId         结果id
     * @return
     */
    private List<OperatingUnitFormItem> queryByCellFormItemFatherId(String cellFormItemFatherId, String cellResultId) {
        Map map = new HashMap<>();
        map.put("cellFormItemFatherId", cellFormItemFatherId);
        if (StringUtils.isBlank(cellResultId)) {
            map.put("cellResultId", "-1");
        } else {
            map.put("cellResultId", cellResultId);
        }
        return operatingUnitFormItemMapper.queryByCellFormItemFatherId(map);
    }

}
